package city.spring.modules.bed.service.impl;

import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.repository.BedRoomRepository;
import city.spring.modules.bed.service.BedRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：房间信息
 *
 * @author 侯坤林
 * @date 2020-04-26 12:01:45
 */
@CacheConfig(cacheNames = {BedRoomServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class BedRoomServiceImpl extends ServiceImpl<BedRoomRepository, BedRoomEntity> implements BedRoomService {

    @Override
    public void saveBedRoom(BedRoomEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateBedRoom(BedRoomEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteBedRoom(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteBedRoom(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public List<BedRoomEntity> getSendAreaBedRoom(String areaId) {
        return baseMapper.getBedRoom(areaId);
    }

    @Override
    public void deleteSendId(String primaryKey) {
        lambdaUpdate().set(BedRoomEntity::getAreaId, null).eq(BedRoomEntity::getId, primaryKey).update();
    }

}
